package com.project.controller;

import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService service ;

    @RequestMapping("land")
    public String land(String userName, String password,HttpServletRequest req) {
             UserBean user = service.land(userName,password);
             if (user!=null){
                 req.getSession().setAttribute("loginUser", user);
                 return "ok";
             }
             return "no";
    }

@RequestMapping("findAll")
    public List<UserBean> findAll() {
        return service.findAll();
    }

@RequestMapping("del")
    public String del(Integer id, HttpServletRequest req) {
        UserBean user = (UserBean)req.getSession().getAttribute("loginUser");
        if (service.del(id,user)){
            return "ok";
        }
        return "no";
    }
    @RequestMapping("findById")
    public UserBean findById(Integer id) {
        return service.findById(id);
    }
@RequestMapping("updateGrade")
    public String updateGrade(Integer id, String grade) {
        service.updateGrade(id,grade);
        return "ok";
    }

@RequestMapping("updatePassword")
    public String updatePassword(Integer id, String password) {
        service.updatePassword(id,password);
        return "ok";
    }
    @RequestMapping("add")
    public String add(String userName, String name,  String grade) {
        UserBean user = new UserBean(userName,name,grade);
        if (service.add(user)){
            return "ok";
        }else {
            return "no";
        }
    }
    @RequestMapping("getLoginUser")
    public UserBean getLoginUser(HttpServletRequest req){
        UserBean user = (UserBean)req.getSession().getAttribute("loginUser");
        return user;
    }
}
